<template>
<div class="recharge-wrap">
    <G_header title="充值" />
    <van-cell-group>
        <van-cell title="当前余额">
            <template slot="right-icon" v-if="userInfo">
                {{userInfo.account.money | money}}
            </template>
        </van-cell>
        <van-field v-model="rechargeMoney" required clearable label="充值余额" placeholder="请输入充值余额" />
    </van-cell-group>
    <div class="go">
        <van-button block size="small" :disabled="rechargeMoney ? false :true" type="primary" @click="pay">下一步</van-button>
    </div>
</div>
</template>

<script>
import wx from 'weixin-js-sdk'
import { Toast } from 'vant'
export default {
    name: "Recharge",
    data() {
        return {
            currentMoney: "0.00",
            rechargeMoney: null,
            is_open: true,
            show: false,
            userInfo: null
        };
    },
    mounted() {
        this.getUserInfos();

    },
    methods: {
        getUserInfos(){
            this.userInfo = this.global.state.userInfo;
        },
        pay(){
          let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
           if (!reg.test(this.rechargeMoney)){
               Toast('请输入正确金额');
           }else if(this.rechargeMoney != Math.floor(this.rechargeMoney)){
               Toast('请输入整数');
           }else{
                let params = {};
                params.token = sessionStorage.getItem("token");
                params.money = this.rechargeMoney;
                this.axios.get('/api/store/user/recharge',{params}).then(res=>{
                    this.wxPay(res.data.data)
                }).catch(err=>{
                    console.log(err);
                })
           }
          
        },
        wxPay(data) {
            let params = {};
            params.token = sessionStorage.getItem("token");
            this.axios.get('/api/wechatofficial/1/share_config/', {
                params
            }).then(res => {
                let configData = res.data.data;
                wx.config({
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: configData.appId, // 必填，公众号的唯一标识
                    timestamp: configData.timestamp, // 必填，生成签名的时间戳
                    nonceStr: configData.nonceStr, // 必填，生成签名的随机串
                    signature: configData.signature, // 必填，签名，见附录1
                    jsApiList: ['chooseWXPay'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                });
                wx.ready(function (){
                    wx.chooseWXPay({
                        timestamp: data.timestamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                        nonceStr: data.nonceStr, // 支付签名随机串，不长于 32 位
                        package: data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=***）
                        signType: data.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                        paySign: data.paySign, // 支付签名
                        success: function (res) {
                            // 支付成功后的回调函数
                            alert(res)
                            alert(res.errMsg)
                            if(res.errMsg == "chooseWXPay:ok"){
                              Dialog({ message: '支付成功' });
                              this.getUserInfo()
                            }else {
                              Dialog({ message: '错误' });
                            }
                            this.getDataList();
                        },
                        fail: function (res) {
                            Dialog({
                                message: res
                            });
                            errorCb(res);

                        }
                    });
                });
                wx.error(function (res) {
                    Dialog({
                        message: res
                    });
                    alert(res)
                    console.log('err', res)
                });
            }).catch(err => {
                console.log(err)
            })
        },
        getUserInfo() {
            let token = sessionStorage.getItem("token");
            let params = {};
            params.token = token;
            this.axios.get('/api/wechatofficial/mine',{params}).then(result => {
                  this.userInfo = result.data.data;
                  this.global.state.userInfo = this.userInfo;
                  this.global.state.storeInfo = this.userInfo.stores;
              })
              .catch(error => {
                  console.log(error)
              });
        }
    }
};
</script>

<style scoped>
.recharge-wrap .go {
    box-sizing: border-box;
    padding: 10px 12px 0;
}
</style>
